﻿// -----------------------------------------------------------------------
// <copyright file="ProvinceRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ProvinceRepository : AbstractRepository<Province>
    {
        public ProvinceRepository()
        {
            base._contextHelper = null;
            base._context = new Context.AGRContext();
        }

        public override IQueryable<Province> GetAll()
        {
            var result = (from x in base._context.Provinces
                          select new Province
                          {
                              Id = x.Id,
                              CountryId = x.CountryId,
                              ProvinceCode = x.ProvinceCode,
                              ProvinceName = x.ProvinceName,
                              Displayed = x.Displayed,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Province entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Province entityToAdd = new Context.Province
            {
                CountryId = entity.CountryId,
                ProvinceCode = entity.ProvinceCode,
                ProvinceName = entity.ProvinceName,
                Displayed = entity.Displayed,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Provinces.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Province entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Province entityToUpdate = base._context.Provinces.Single(x => x.ProvinceName.Equals(entity.ProvinceName));

            entityToUpdate.CountryId = entity.CountryId;
            entityToUpdate.ProvinceCode = entity.ProvinceCode;
            entityToUpdate.ProvinceName = entity.ProvinceName;
            entityToUpdate.Displayed = entity.Displayed;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Province entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Province entityToDelete = base._context.Provinces.Single(x => x.ProvinceName.Equals(entity.ProvinceName));

            base._context.Provinces.DeleteOnSubmit(entityToDelete);
        }
    }
}
